package com.example.urmsadmin.Util;

import java.util.regex.Pattern;

/**
 * @program:
 * @description: 校验工具类
 * @author: helei
 * @create: 2022-7-19 15:23:37
 **/
public class ValidatorUtil {
    /**
     * 正则表达式：验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z0-9]\\w{5,20}$";

    /**
     * 正则表达式：验证姓名
     */
    public static final String REGEX_TRUENAME = "^[\\u4E00-\\u9FA5A-Za-z]{1,50}$";

    /**
     * 正则表达式：验证密码
     */
    public static final String REGEX_PASSWORD = "^[A-Za-z0-9]{5,20}$";

    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "^((0\\d{2,3}-\\d{7,8})|(1[345789]\\d{9}))$";

    /**
     * 正则表达式：验证邮箱
     */
    public static final String REGEX_EMAIL = "^(\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*)$";

    /**
     * 正则表达式：验证备注
     */
    public static final String REGEX_REMARK = "^.{0,500}$";

    /**
     * 正则表达式：验证角色名
     */
    public static final String REGEX_ROLENAME = "^.{0,100}$";

    /**
     * 正则表达式：验证角色编码
     */
    public static final String REGEX_ROLEKEY = "^.{0,50}$";

    /**
     * 正则表达式：验证菜单名称
     */
    public static final String REGEX_MENUNAME = "^.{0,20}$";

    /**
     * 校验用户名
     *
     * @param userName
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUserName(String userName) {
        if(userName == null || "".equals(userName)){
            return false;
        }
        return Pattern.matches(REGEX_USERNAME, userName);
    }

    /**
     * 校验姓名
     *
     * @param trueName
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isTrueName(String trueName) {
        if(trueName == null || "".equals(trueName)){
            return false;
        }
        return Pattern.matches(REGEX_TRUENAME, trueName);
    }

    /**
     * 校验密码
     *
     * @param password
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isPassword(String password) {
        if(password == null || "".equals(password)){
            return false;
        }
        return Pattern.matches(REGEX_PASSWORD, password);
    }

    /**
     * 校验手机号
     *
     * @param phonenumber
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isPhonenumber(String phonenumber) {
        if(phonenumber == null || "".equals(phonenumber)){
            return true;
        }
        return Pattern.matches(REGEX_MOBILE, phonenumber);
    }

    /**
     * 校验邮箱
     *
     * @param email
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isEmail(String email) {
        if(email == null || "".equals(email)){
            return true;
        }
        return Pattern.matches(REGEX_EMAIL, email);
    }

    /**
     * 校验备注
     *
     * @param remark
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isRemark(String remark) {
        if(remark == null || "".equals(remark)){
            return true;
        }
        return Pattern.matches(REGEX_REMARK, remark);
    }

    /**
     * 校验角色名
     *
     * @param roleName
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isRoleName(String roleName) {
        if(roleName == null || "".equals(roleName)){
            return false;
        }
        return Pattern.matches(REGEX_ROLENAME, roleName);
    }

    /**
     * 校验角色编码
     *
     * @param roleKey
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isRoleKey(String roleKey) {
        if(roleKey == null || "".equals(roleKey)){
            return false;
        }
        return Pattern.matches(REGEX_ROLEKEY, roleKey);
    }

    /**
     * 校验菜单名称
     *
     * @param menuName
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isMenuName(String menuName) {
        if(menuName == null || "".equals(menuName)){
            return false;
        }
        return Pattern.matches(REGEX_MENUNAME, menuName);
    }


}
